package com.woniuxy.afterloan.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.afterloan.entity.AfterLoans;
import com.woniuxy.afterloan.service.AfterLoanService;
import com.woniuxy.afterloan.service.impl.MailServiceImpl;
import com.woniuxy.commons.annotation.RequirePerms;
import com.woniuxy.commons.entity.AfterLoan;
import com.woniuxy.commons.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("afterLoan")
@RestController
public class AfterLoanController {

    @Autowired
    AfterLoanService afterLoanService;

    @PostMapping("list")
    public ResponseResult getByCondition(@RequestBody AfterLoans afterLoans){
        Integer current = afterLoans.getCurrent();
        if (current == null) {
            current = 1;
        }

        QueryWrapper<AfterLoans> queryWrapper = new QueryWrapper<>();
        if (afterLoans.getOverdue() != null && afterLoans.getOverdue().length() != 0) {
            queryWrapper.eq("overdue", afterLoans.getOverdue());
        }
        if (afterLoans.getUname() != null && afterLoans.getUname().trim().length() != 0) {
            queryWrapper.like("uname", afterLoans.getUname());
        }
        if (afterLoans.getDate() != null && afterLoans.getDate().length == 2) {
            System.out.println(afterLoans.getDate()[0]+"+"+afterLoans.getDate()[1]);
            queryWrapper.between("due_date", afterLoans.getDate()[0],afterLoans.getDate()[1]);
        }
        Page<AfterLoans> page = new Page<>(current, 3);


        ResponseResult responseResult = new ResponseResult();
        responseResult.setCode(200);
        responseResult.setData(afterLoanService.page(page, queryWrapper));

        return responseResult;
    }

    @Autowired
    @Qualifier("serdbyemail")
    private MailServiceImpl mailService;

    @PostMapping("mail")
    public ResponseResult mail(@RequestBody AfterLoans[] afterLoans){
        ResponseResult result=new ResponseResult();
        for(AfterLoans afterLoan:afterLoans){
            if(afterLoan!=null) {
                String sender="768291370@qq.com";   //这个是发送人的邮箱
                String receiver=afterLoan.getEmail();  //这个是接受人的邮箱
                String title="还款通知";    //标题
                String text = "";
                if(afterLoan.getLetterContent() == null) {
                    text = "尊敬的用户提醒您,当期还款"+afterLoan.getShould()/afterLoan.getLoanDate()+"元";
                }else {
                    text = afterLoan.getLetterContent();
                }
                mailService.send(sender, receiver, title, text);
                AfterLoan afterLoan1 = new AfterLoan();
                afterLoan1.setAid(afterLoan.getAid());
                afterLoan1.setCollection("是");
                afterLoanService.update(afterLoan1);
            }
        }

        result.setCode(200);
        return result;
    }
    @RequirePerms("user:del")
    @PostMapping("delete")
    public ResponseResult delete(@RequestBody AfterLoans[] afterLoans){
        ResponseResult result=new ResponseResult();
        for(AfterLoans afterLoan:afterLoans){
            if(afterLoan!=null) {
                afterLoanService.delete(afterLoan.getAid());
            }
        }
        result.setCode(200);
        return result;
    }
}
